<?php
/**
 * CEOfox
 *
 * @copyright  Copyright 2011-2012 CEOfox, Inc.
 * @license    http://ceofox.com/pages/license
 * @version    $Id: follower.class.php 57087 2013-05-24 09:40:17 $
 * @author     CEOfox, Inc.
 */
?>
<?php
defined('PHPFOX') or exit('NO DICE!');

class Follow_Component_Block_Follower extends Phpfox_Component
{ 
    public function process()
    {
        Phpfox::isUser(true);
        $bIsCurrentUser = false;
        $iPage = $this->getParam('page', 1);
        $sKeyword = $this->getParam('keyword', '');
        $iOffset = ($iPage - 1) * phpfox::getParam('follow.showeachtime');
        $sType = $this->getParam('type_id', 'user');
        if($sType == 'user')
        {
            if(!$aItem = $this->getParam('aUser'))
            {
                $aItem = Phpfox::getService('user')->get(Phpfox::getUserId());
                $bIsCurrentUser = true;
            }
        }
        else
        {
            if(!Phpfox::hasCallback($sType, 'getActivityFeed') || !$aParentModule = $this->getParam('aParentModule', false))
            {
                return false;
            }
            $aItem = Phpfox::callback($sType . '.getActivityFeed', $aParentModule, null, true);
            if($sType == 'pages')
            {
                if(Phpfox::getService('pages')->isAdmin($aParentModule['item_id']))
                {
                    $bIsCurrentUser = true;
                }
            }
            else
            {
                if($aItem['user_id'] == Phpfox::getUserId())
                {
                    $bIsCurrentUser = true;
                }
            }
        }
        
        $aItem['item_id'] = $aItem[(($sType == 'pages') ? 'page': $sType).'_id'];
        $aResult = array();
        $sKeyword = '';
        $iTotalFollower = 0;
        $aFollowers = $this->getParam('aItems', array());
        
        $bIsBlockAll = phpfox::getservice('follow')->isBlockAll($aItem[(($sType == 'pages') ? 'page': $sType).'_id'], $sType);
        
        if(!$bIsBlockAll)        
        {
            if(!$sKeyword = $this->getParam('sKeyword'))
            {
                list($iTotalFollower, $aFollowers) = Phpfox::getService('follow')->getFollower($aItem[(($sType == 'pages') ? 'page': $sType).'_id'], $iOffset, phpfox::getParam('follow.showeachtime'), $sType, $sKeyword);
            }
            if(!empty($aFollowers))
            {
                foreach($aFollowers as $aFollower)
                {

                    $aTemp = phpfox::getservice('user')->get($aFollower['user_id']);
                    if($aFollower['user_id'] == Phpfox::getUserId() && $sType == 'user')
                    {
                        $aTemp['canFollow'] = 0;
                        $aTemp['isFollow'] = 0;
                    }
                    else
                    {
                        if(Phpfox::getService('follow')->canFollow($aFollower['user_id'], $sType))
                        {
                            $aTemp['isFollow'] = Phpfox::getService('follow')->isFollow(Phpfox::getUserId(),$aFollower['user_id'], $sType);
                            $aTemp['canFollow'] = 1;
                        }
                        else
                        {
                            $aTemp['isFollow'] = 0;
                            $aTemp['canFollow'] = 0;
                        }
                    }
                    $aResult[] = $aTemp;
                }
                
            }    
            
            $this->template()->assign(array(
                'aItem' => $aItem,
                'iPage' => $iPage,
                'sType' => $sType,
                'bIsCurrentUser' => $bIsCurrentUser,
                'aFollowers' => $aResult,
                'sKeyword' => $sKeyword,
                'sBlockType' => ($this->getParam('sBlockType') ? $this->getParam('sBlockType') : 'follower'),
                'bIsBlockAll' => $bIsBlockAll,
                'iCurrentFollower' => isset($aFollowers)? count($aFollowers):0,
                'sFullControllerName'  => Phpfox::getLib('module')->getFullControllerName(),
                'iTotalFollower' => ($this->getParam('iCnt') ? $this->getParam('iCnt') : $iTotalFollower),
            ));
        }
    }
    public function clean()
    {        
        (($sPlugin = Phpfox_Plugin::get('follow.component_block_follower_clean')) ? eval($sPlugin) : false);
    }
}

?>
